﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CrossQuery;

namespace CubePresentation
{
    /// <summary>
    /// Interaction logic for CrossSearchWindow.xaml
    /// </summary>
    public partial class CrossSearchWindow : Window
    {
        CrossQuery.FreeQuery crossQuery;
        public CrossSearchWindow(CrossQuery.FreeQuery cq)
        {
            InitializeComponent();
            Title = cq.DisplayName;
            crossQuery = cq;
            int ppos = 0;
            int rows = controlGrid.RowDefinitions.Count;
            int cols = controlGrid.ColumnDefinitions.Count;
            for (int i = 0; i < cq.ParameterTypes.Count; ++i)
            {
                Label label = new Label();
                label.Content = cq.ParameterDisplayNames[i];
                label.SetValue(Grid.RowProperty, ppos / cols);
                label.SetValue(Grid.ColumnProperty, ppos % cols);
                label.SetValue(Control.HorizontalAlignmentProperty, HorizontalAlignment.Center);
                label.SetValue(Control.VerticalAlignmentProperty, VerticalAlignment.Center);
                label.SetValue(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
                label.SetValue(Control.VerticalContentAlignmentProperty, VerticalAlignment.Center);
                controlGrid.Children.Add(label);
                ++ppos;
                switch (cq.ParameterTypes[i].Type)
                {
                    case "Date":
                        DatePicker dp = new DatePicker();
                        dp.SetValue(Grid.RowProperty, ppos / cols);
                        dp.SetValue(Grid.ColumnProperty, ppos % cols);
                        dp.SelectedDateChanged += datePicker_SelectDateChanged;
                        dp.SetValue(Control.HorizontalAlignmentProperty, HorizontalAlignment.Center);
                        dp.SetValue(Control.VerticalAlignmentProperty, VerticalAlignment.Center);
                   
                        dp.SelectedDate = dp.DisplayDate = DateTime.Now;
                        controlGrid.Children.Add(dp);
                        break;
                    case "Combo":
                        ComboBox cb = new ComboBox();
                        cb.SetValue(Grid.RowProperty, ppos / cols);
                        cb.SetValue(Grid.ColumnProperty, ppos % cols);
                        cb.SetValue(Control.HorizontalAlignmentProperty, HorizontalAlignment.Center);
                        cb.SetValue(Control.VerticalAlignmentProperty, VerticalAlignment.Center);
                        cb.SetValue(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
                        cb.SetValue(Control.VerticalContentAlignmentProperty, VerticalAlignment.Center);
                        controlGrid.Children.Add(cb);
                        // add data to combo box
                        ComboType comboType = (ComboType)cq.ParameterTypes[i];
                        for (int j = 0; j < comboType.Count; ++j)
                        {
                            cb.Items.Add(comboType[j]);
                        }
                        if (comboType.Count > 0)
                        {
                            cb.SelectedIndex = 0;
                            comboType.Value = ((ComboNode)cb.Items[0]).Code;
                        }
                        cb.SelectionChanged += ComboBox_SelectionChanged;
                        break;
                    default:
                        break;
                }
                ++ppos;
            }
        }

        private void datePicker_SelectDateChanged(object sender, SelectionChangedEventArgs e)
        {
            DatePicker dp = (DatePicker)sender;
            int row = (int)dp.GetValue(Grid.RowProperty);
            int col = (int)dp.GetValue(Grid.ColumnProperty);
            Label label = (Label)controlGrid.Children.Cast<UIElement>().First(ee => Grid.GetRow(ee) == row && Grid.GetColumn(ee) == col - 1);
            crossQuery[(string)label.Content] = ((DateTime)dp.SelectedDate).ToString();
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = (ComboBox)sender;
            int row = (int)cb.GetValue(Grid.RowProperty);
            int col = (int)cb.GetValue(Grid.ColumnProperty);
            Label label = (Label)controlGrid.Children.Cast<UIElement>().First(ee => Grid.GetRow(ee) == row && Grid.GetColumn(ee) == col - 1);
            crossQuery[(string)label.Content] = ((ComboNode)cb.SelectedItem).Code;
        }

        enum data_types { vertical, horizontal };
        private void excuteButton_Click_1(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.DataVisualization.Charting.SeriesChartType type = Chart.Pie;
            data_types data_type = data_types.vertical;
            if (crossQuery.ChartType.Length > 1){
                if (crossQuery.ChartType.Substring(0, 1).Equals("v"))
                {
                    data_type = data_types.vertical;
                }
                else if (crossQuery.ChartType.Substring(0, 1).Equals("h"))
                {
                    data_type = data_types.horizontal;
                }
                switch (crossQuery.ChartType.Substring(1))
                {
                    case "pie":
                        type = Chart.Pie;
                        break;
                    case "bar":
                        type = Chart.Bar;
                        break;
                    case "doughnut":
                        type = Chart.Doughnut;
                        break;
                    case "line":
                        type = Chart.Line;
                        break;
                    default:
                        break;
                }
            }
            List<List<string>> rst = crossQuery.Execute();
            List<double> values = new List<double>();
            List<string> fields = new List<string>();
            if (data_type == data_types.vertical)
            {
                for (int i = 0; i < rst.Count; ++i)
                {
                    values.Add(System.Convert.ToDouble(rst[i][1]));
                    fields.Add(rst[i][0]);
                }

            }
            else
            {
                for (int i = 0; i < rst[0].Count; ++i)
                {
                    values.Add(System.Convert.ToDouble(rst[0][i]));
                    fields.Add(crossQuery.OutputFieldDisplayNames[i]);
                }


            }
            chart.setChart(Title, fields.ToArray(), values.ToArray(), type);
        }
        


        
    }
}
